﻿function LoadEmploymentTypeAutoComplete() {
    cacheEmpData = new Array();
    employmentCount = 0;

    $("button[id^='EmploymentAdd']").live("click", null, function () {
        employmentCount++;
        var txtTagsID = "EmploymentText" + skillsCount;
        // var btnAddID = "add" + window.count;
        var btnDelID = "EmploymentDel" + skillsCount;
        var ul = $("#ulEmploymentType");
        if (employmentCount <= 2) {
            var li = $("<li class='li-style-employment'></li>")
                .append($("<input id='" + txtTagsID + "' />"
                 //   + "<button id='" + btnAddID + "' >Add</button>"
                    + "<button id='" + btnDelID + "' class=\"buttonDel\" >X</button>"));
            li.appendTo(ul);
        }
        return false;
    });

    $("button[id^='EmploymentDel']").live("click", null, function () {
        var li = $(this).parent();
        li.remove();
        employmentCount--;
        return false;
    });

    $("input:text[id^='EmploymentText']").live("focus.autocomplete", null, function () {
        $(this).autocomplete({
            source: function (request, response) {
                if (cacheEmpData["employment"] == null) {
                    $.ajax({
                        type: "POST",
                        url: "HandlerSearchCV.ashx",                        
                        data: { 'search': 'employmenttype' },
                        dataType: "json",
                        success: function (data) {
                            data.sort();
                            cacheEmpData["employment"] = data;

                            var re = $.ui.autocomplete.escapeRegex(request.term);
                            var matcher = new RegExp("^" + re, "i");
                            var a = $.grep(cacheEmpData["employment"], function (item, index) {
                                return matcher.test(item);
                            })

                            response(a);
                        },
                        error: function (response) {
                            alert(response.toString());
                        }
                    })
                }
                else {
                    var re = $.ui.autocomplete.escapeRegex(request.term);
                    var matcher = new RegExp("^" + re, "i");
                    var a = $.grep(cacheEmpData["employment"], function (item, index) {
                        return matcher.test(item);
                    });
                    if (typeof a === 'undefined') {
                        response("");
                    }
                    else
                        response(a);
                }                         
            },
            select: function (event, ui) {
                if (ui.item) {
                    
                    var pageSize = $('#ctl00_MainContent_ddlPageSize option:selected').text().trim();

                    var jobTitleText = "";
                    $("#JobTitleSearch :text").each(function () {
                        jobTitleText += $(this).val() + "$";
                    });

                    var jobSeekerPlaceText = "";
                    $("#Place :text").each(function () {
                        jobSeekerPlaceText += $(this).val() + "$";
                    });

                    var jobCategoryText = "";
                    $("#Category :text").each(function () {
                        jobCategoryText += $(this).val() + "$";
                    });


                    var jobSeekerEmploymentType = "";
                    $("#EmploymentType :text").each(function () {
                        jobSeekerEmploymentType += $(this).val() + "$";
                    });


                    var jobSeekerQualification = "";
                    $("#Qualification :text").each(function () {
                        jobSeekerQualification += $(this).val() + "$";
                    });

                    var jobSeekerSkills = "";
                    $("#Skills :text").each(function () {
                        jobSeekerSkills += $(this).val() + "$";
                    });

                    var selectedUpdateTime = $('#ctl00_SearchContainer_ddlCvUpdate option:selected').text().trim();
                    var selectedExperience = $('#ctl00_SearchContainer_DropDownListExperience option:selected').text().trim();

                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "SearchCV.aspx/getByEmploymentType",
                        data: JSON.stringify({ 'search': ui.item.value, 'pageSize': pageSize, 'updateTime': selectedUpdateTime, 'experience': selectedExperience, 'jobTitle': jobTitleText, 'location': jobSeekerPlaceText, 'category': jobCategoryText, 'employmentType': jobSeekerEmploymentType, 'qualification': jobSeekerQualification, 'skills': jobSeekerSkills }),
                        datatype: "json",
                        success: function (data) {
                            if (data.d == "")
                                $("[id*=JobSeekerTableData] tr").remove()
                            else {
                                var xmlDoc = $.parseXML(data.d);
                                var xml = $(xmlDoc);
                                var customers = xml.find("GridData");
                                var pager = xml.find("PagerData");

                                if (pager.find("RecordCount").text() != "0") {

                                    $("[id*=JobSeekerTableData] tr").remove();
                                    var table = $("[id*=JobSeekerTableData]");
                                    var i = 0;
                                    $.each(customers, function () {
                                        var customer = $(this);
                                        i++;
                                        /// repeater item template FirstName,LastName
                                        row1 = "<tr> <td> "
                                           + $(this).find("FirstName").text() + "," + $(this).find("LastName").text()
                                           + "</td><td>CV updated</td><td>"
                                           + $(this).find("cvLastUpdated").text()
                                           + " </td></tr>";
                                        /// repeater item template Degree,Institution
                                        row2 = "<tr> <td> "
                                            + $(this).find("Degree").text() + "," + $(this).find("InstitutionName").text()
                                           + "</td><td>Experience</td><td>"
                                           + $(this).find("totalExperience").text()
                                           + " </td></tr>";
                                        /// repeater item template Job title,Company Name
                                        row3 = "<tr> <td> "
                                            + $(this).find("Title").text() + "," + $(this).find("CompanyName").text()
                                           + "</td><td></td><td>"
                                  + " <input id='ctl00_MainContent_jobseekerRepeater_ctl0" + i + "_jobseekerID' type='hidden' value='" + $(this).find("JobSeekerID").text() + "' /> "
                                           + " </td></tr>";
                                        /// repeater item ViewDetails Button
                                        row4 = "<tr><td>&nbsp;</td><td>&nbsp; </td><td>"
                                            + "<a href='SearchCvDetails.aspx?id=" + $(this).find("JobSeekerID").text() + "'"
                                            + "class='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only' role='button'><span class='ui-button-text'>View Details</span></a>"
                                            + "</td></tr>";
                                        /// repeater Top skils,Company experience
                                        row5 = "<tr>"
                                            + "<td>"
                                            + "<table style='width: 100%;'> <tr> <td>Top Skills </td> </tr> <tr> <td>"
                                                + $(this).find("SkillOne").text() + "</td></tr> <tr><td>"
                                                + $(this).find("SkillTwo").text() + "</td></tr> <tr><td>"
                                                + $(this).find("SkillThree").text() + "</td></tr> <tr><td>"
                                                + $(this).find("SkillFour").text() + "</td></tr> </table>"
                                            + "</td>"
                                            + "<td> <table style='width: 100%;'><tr> <td>Company</td> <td>Experience</td></tr>"
                                            + "<tr><td>"
                                            + $(this).find("company1").text() + "</td> <td>"
                                            + $(this).find("companyExp1").text()
                                            + "</td> </tr>"
                                            + "<tr><td>"
                                            + $(this).find("company2").text() + "</td> <td>"
                                            + $(this).find("companyExp2").text()
                                            + "</td> </tr>"
                                            + "<tr><td>"
                                            + $(this).find("company3").text() + "</td> <td>"
                                            + $(this).find("companyExp3").text()
                                            + "</td> </tr>"
                                            + "<tr> <td></td> <td></td> </tr></table>"
                                            + "</td> <td>&nbsp; </td></tr>"
                                            ///// line break/////////
                                            + "<tr> <td colspan='3'> <hr /> </td></tr>";

                                        /// repeater item template FirstName,LastName

                                        table.append(row1);
                                        table.append(row2);
                                        table.append(row3);
                                        table.append(row4);
                                        table.append(row5);

                                        var pager = xml.find("PagerData");
                                        $(".Pager").ASPSnippets_Pager({
                                            ActiveCssClass: "current",
                                            PagerCssClass: "pager",
                                            PageIndex: 1,
                                            PageSize: $('#ctl00_MainContent_ddlPageSize option:selected').text().trim(),
                                            RecordCount: parseInt(pager.find("RecordCount").text())
                                        });
                                    });
                                }
                                else {
                                    window.scrollTo(0, 0);
                                    $("#NoRecordsMessage").css({ display: "block" });
                                    $("#NoRecordsMessage").fadeIn(1000).fadeOut(1000).fadeIn(1000).fadeOut(1000).fadeIn(1000).fadeOut(1000);
                                }
                            }
                        },
                        error: function (response, error, xhr) {
                            // alert(xhr.toString());
                        },
                        complete: function (xhr, status) {
                            var msg = xhr.responseText;
                            //alert(msg);
                        }


                    })
                }
            },

            minLength: 1,
            delay: 1

        });
        $(this).autocomplete("search");
    });
}